package zconf

import (
	"gopkg.in/yaml.v2"
	"os"
)

type MySql struct {
	MysqlHost   string `yaml:"mysql_host"`
	MysqlPort   string `yaml:"mysql_port"`
	MysqlUser   string `yaml:"mysql_user"`
	MysqlPasswd string `yaml:"mysql_passwd"`
	MysqlName   string `yaml:"mysql_name"`
}

type Mqtt struct {
	MqUser   string `yaml:"mq_user"`
	MqPasswd string `yaml:"mq_passwd"`
	MqHost   string `yaml:"mq_host"`
	MqPort   int    `yaml:"mq_port"`
}

type Redis struct {
	RedisHost   string `yaml:"redis_host"`
	RedisPort   string `yaml:"redis_port"`
	RedisPasswd string `yaml:"redis_passwd"`
	RedisDb     int    `yaml:"redis_db"`
}

type Web struct {
	WebPort  int `yaml:"web_port"`
	WebPort2 int `yaml:"web_port2"`
	WebPort3 int `yaml:"web_port3"`
}

type Tcp struct {
	DeviceType string `yaml:"device_type"`
	TcpPort    int    `yaml:"tcp_port"`
	TcpPort2   int    `yaml:"tcp_port2"`
	Router     int    `yaml:"router"`
	Domain     string `yaml:"domain"`
}

type Config struct {
	MySql  `yaml:"mysql"`
	Mqtt   `yaml:"mqtt"`
	Web    `yaml:"web"`
	Redis  `yaml:"redis"`
	Tcp    `yaml:"tcp"`
	Url `yaml:"url"`
}

type Url struct {
	PaymentNotifyUrl string `yaml:"payment_notify_url"`
	BatteryPlatformUrl string `yaml:"battery_platform_url"`
}

const (
	ConfigPath = "conf/cfg.yml"
)

func Read(file string) (cfg Config, e error) {
	if file == "" {
		file = ConfigPath
	}
	var yamlFile []byte
	if yamlFile, e = os.ReadFile(file);e != nil {
		return
	}

	if e = yaml.Unmarshal(yamlFile, &cfg); e != nil {
		return
	}
	return
}
